package com.lightsh.viewpager;

import java.security.spec.MGF1ParameterSpec;
import java.util.ArrayList;

import com.lightsh.common.BottomBarLayout;
import com.lightsh.common.PaintItem;
import com.lightsh.common.PaintItemView;
import com.lightsh.db.DbDataHelper;
import com.lightsh.db.MyDbHelper;
import com.lightsh.handwritten.FingerPaintActivity;
import com.lightsh.handwritten.HandwrittingActivity;
import com.lightsh.handwritten.R;
import com.lightsh.list.AbstractListItem;
import com.lightsh.list.CommonListAdapter;
import com.lightsh.list.OnItemSelectListener;
import com.lightsh.utility.Constants;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

public class PaintListFragment extends BaseFragment implements AdapterView.OnItemClickListener,
OnClickListener{
	private static final String TAG = "PaintListFragment";
	private static final boolean DEBUG = Constants.DEBUG;
	private static void logD(String tip){
		if(DEBUG){
			Log.d(TAG, "" + tip);
		}
	}
	private View mRootView;
	private GridView mListPaint;
	private BaseAdapter mAdapter;
	private ArrayList<PaintItem> mDataList = new ArrayList<PaintItem>();
	
	private BottomBarLayout mBottomBar;
	
	private View mNewLayout;
	private static final int NUM_COLUMNS = 2;
	
	private static final int REQUEST_CODE_NEW = 1;
	private static final int REQUEST_CODE_PAINT = 2;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		logD("onCreateView , savedInstanceState = " + savedInstanceState + " mRootView = " + mRootView);
		mRootView = inflater.inflate(R.layout.paint_list_layout, null);
		initWidget();
		return mRootView;
	}
	

	@Override
	public void onAttach(Activity activity) {
		logD("onAttach");
		super.onAttach(activity);
	}


	@Override
	public void onDestroyView() {
		logD("onDestroyView");
		super.onDestroyView();
	}


	@Override
	public void onDetach() {
		logD("onDetach");
		super.onDetach();
	}


	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		logD("onActivityCreated, savedInstanceState = " + savedInstanceState);
		super.onActivityCreated(savedInstanceState);
	}


	@Override
	public void onCreate(Bundle savedInstanceState) {
		logD("onCreate");
		super.onCreate(savedInstanceState);
		initAdapter();
		getPaintList();
	}
	private void initWidget(){
		mListPaint = (GridView) mRootView.findViewById(R.id.paintList);
		mListPaint.setOnItemClickListener(this);
		mListPaint.setNumColumns(NUM_COLUMNS);
		mListPaint.setAdapter(mAdapter);
		initBottomBar();
		
		mNewLayout = mRootView.findViewById(R.id.newLayout);
		mNewLayout.setOnClickListener(this);
		TextView newText = (TextView) mRootView.findViewById(R.id.newText);
		newText.setText(R.string.newPaint);
	}

	private void initAdapter(){
		mAdapter = new CommonListAdapter(getActivity(), mDataList, PaintItemView.class, new OnItemSelectListener() {
			
			@Override
			public void onItemSelect(int index, boolean bSelected) {
				mDataList.get(index).setSelected(bSelected);
				checkShowBottomBar();
			}
		});
		PaintItemView.initSize(getActivity(), NUM_COLUMNS);
	}
	private void getPaintList(){
		DbDataHelper.getAllPaintList(getActivity(), mDataList);
		mAdapter.notifyDataSetChanged();
	}
	
	@Override
	public void onResume() {
		logD("onResume");
		super.onResume();
	}

	@Override
	public void onPause() {
		logD("onPause");
		super.onPause();
	}

	@Override
	public void onDestroy() {
		logD("onDestroy");
		super.onDestroy();
	}


	@Override
	public boolean onBackPressed() {
		if(mBottomBar.isBottomBarVisible()){
			setAllItemSelect(false);
			mBottomBar.hideBottomBar();
			return true;
		}
		return super.onBackPressed();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		startPaintActivity(mDataList.get(position).getId());
	}
	private void startPaintActivity(long id){
		Intent intent = new Intent(getActivity(), FingerPaintActivity.class);
		if(id != -1){
			intent.putExtra(FingerPaintActivity.EXTRA_PAINT_ID, id);
		}
		startActivityForResult(intent, REQUEST_CODE_PAINT);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.newLayout:
			startPaintActivity(-1);
			break;
		}
		
	}
	

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == Activity.RESULT_OK){
			switch (requestCode) {
			case REQUEST_CODE_NEW:
				break;
			case REQUEST_CODE_PAINT:
				handlePaintChange();
				break;
			default:
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	private void handlePaintChange(){
		mDataList.clear();
		getPaintList();
		checkShowBottomBar();
	}
	
	private void initBottomBar(){
		mBottomBar = new BottomBarLayout(getActivity());
		mBottomBar.init(mRootView.findViewById(R.id.bottomBarLayout));
		mBottomBar.updateBottomBarListener(mOnBottomBtnClickListener);
	}
	private void checkShowBottomBar(){
		int selectCount = 0;
		for(AbstractListItem item : mDataList){
			if(item.isSelected()){
				selectCount++;
			}
		}
		if(selectCount > 0){
			mBottomBar.setShowBottomBar(selectCount == mDataList.size());
		}else{
			mBottomBar.hideBottomBar();
		}
	}
	
	private OnClickListener mOnBottomBtnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.btnLeft:{
				deleteSelectedItem();
			}
				break;
			case R.id.btnRight:{
				Button button = (Button) v;
				if(getString(R.string.selectAll).equals(button.getText())){
					setAllItemSelect(true);
					mBottomBar.setShowBottomBar(true);
				}else{
					setAllItemSelect(false);
					mBottomBar.hideBottomBar();
				}
			}
				 break;
			}
			
		}
	};
	private void deleteSelectedItem(){
		MyDbHelper db = new MyDbHelper(getActivity());
		boolean bChange = false;
		try{
			final int count = mDataList.size();
			PaintItem item;
			for(int i = count -1; i >= 0; --i){
				item = mDataList.get(i);
				if(item.isSelected()){
					boolean bSuccess = db.deletePaintDataAndStrokes(item.getId());
					if(bSuccess){
						mDataList.remove(i);
						bChange = true;
					}
				}
			}
		}finally{
			if(db != null){
				db.close();
			}
		}
		if(bChange){
			mAdapter.notifyDataSetChanged();
			checkShowBottomBar();
		}
	}
	private void setAllItemSelect(boolean bSelect){
		for(AbstractListItem item : mDataList){
			if(item.isSelected() != bSelect){
				item.setSelected(bSelect);
			}
		}
		mAdapter.notifyDataSetChanged();
	}
}
